﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Al_FrameWork
{
    /// <summary>
    /// 事件管理
    /// </summary>
    public class MessageCenter : Singleton<MessageCenter>
    {
        // 消息委托
        public delegate void messageDelHandle(MessageData message);

        // 消息字典
        private Dictionary<MessageCenterEventType, messageDelHandle> messageMap =
            new Dictionary<MessageCenterEventType, messageDelHandle>();

        /// <summary>
        /// 注册监听
        /// </summary>
        public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle)
        {
            if (handle == null) return;

            // 把事件添加到对应的委托中
            messageDelHandle myHandle = null;
            messageMap.TryGetValue(messageType, out myHandle);

            if (myHandle != null)
            {
                Delegate[] HandleList = myHandle.GetInvocationList();

                foreach (messageDelHandle item in HandleList)
                {
                    if (item == handle)
                    {
                        GlobalFunctions.Err(
                            $"MessageCenter RegisterListener Type  ---  <color=yellow>{messageType.ToString()}</color>  ---  Has Registed Function  ---  <color=yellow>{handle.Method.Name}</color>  ---");
                        return;
                    }
                }
            }

            messageMap[messageType] = (messageDelHandle)Delegate.Combine(myHandle, handle);
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)
        {
            if (handle == null) return;
            messageMap[messageType] = (messageDelHandle)Delegate.Remove(messageMap[messageType], handle);
        }

        /// <summary>
        /// 清空消息
        /// </summary>
        public void Clear()
        {
            messageMap.Clear();
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="messageName">消息类型 </param>
        /// <param name="body"> 发送消息主体 </param>
        public void SendMessage(MessageCenterEventType messageType, object body = null)
        {
            messageDelHandle handle;
            if (messageMap.TryGetValue(messageType, out handle))
            {
                MessageData evt = new MessageData(messageType, body);
                try
                {
                    if (handle != null)
                    {
                        handle(evt);
                    }
                }
                catch (Exception e)
                {
                    GlobalFunctions.Err("错误============================{0}", e.Message);
                }
            }
        }

        #region 枚举类型接口

        #region MessageType

        //public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle)
        //{
        //    RegisterListener(messageType, handle);
        //}
        //public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)
        //{
        //    RemoveListener(messageType, handle);
        //}
        //public void SendMessage(MessageCenterEventType messageType, object body = null)
        //{
        //    SendMessage(messageType, body);
        //}

        #endregion


        #region BattleEvent

        //public void RegisterListener(BattleEvent messageType, messageDelHandle handle)
        //{
        //    RegisterListener((int)messageType, handle);
        //}
        //public void RemoveListener(BattleEvent messageType, messageDelHandle handle)
        //{
        //    RemoveListener((int)messageType, handle);
        //}
        //public void SendMessage(BattleEvent messageType, object body = null)
        //{
        //    SendMessage((int)messageType, body);
        //}

        #endregion


        #region ProtocolEvent

        //public void RegisterListener(ProtocolEvent messageType, messageDelHandle handle)
        //{
        //    RegisterListener((int)messageType, handle);
        //}
        //public void RemoveListener(ProtocolEvent messageType, messageDelHandle handle)
        //{
        //    RemoveListener((int)messageType, handle);
        //}
        //public void SendMessage(ProtocolEvent messageType, object body = null)
        //{
        //    SendMessage((int)messageType, body);
        //}

        #endregion

        #endregion
    }
}